package cn.itcast.bos.web.action;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.util.location.Location;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.StandardService;

@Namespace("/")
@ParentPackage("json-default")
@Actions
// 指定模型对象
@Controller
@Scope("prototype") // 多例
public class StandardAction extends ActionSupport implements ModelDriven<Standard> {
	// 对service对象进行注解
	@Autowired
	private StandardService standardService;
	// 模型驱动
	private Standard standard = new Standard();

	@Override
	public Standard getModel() {
		return standard;
	}

	@Action(value = ("standard_save"), results = {
			@Result(name = "success", type = "redirect", location = "/pages/base/standard.html") })
	public String Save() {
		System.out.println("添加数据");
		// 将得到的数据交给service
		standardService.save(standard);
		return SUCCESS;
	}
	private int page;
	private int rows;
	public void setPage(int page) {
		this.page = page;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}

	@Action(value=("standard_pageQuery"),results={@Result(name="success",type="json")})
	public String pageQuery(){
		Pageable pageable =  new PageRequest(page-1,rows);
		Page<Standard> page =standardService.findPageDate(pageable);
		//创建一个map集合将数据进行封装
		Map<String,Object> result = new  HashMap<String, Object>();
		result.put("total", page.getTotalElements());
		result.put("rows", page.getContent());
		//将map数据写到json数据当中来
		ActionContext.getContext().getValueStack().push(result);
		return SUCCESS;
	}
	@Action(value="standard_findAll",results={@Result(name="success",type="json")})
	public String findAll(){
		List<Standard> standards = standardService.findAll();
		ActionContext.getContext().getValueStack().push(standards);
		return SUCCESS;
	}
	/*//作废操作
	private String ids;
	private String idss;
	public String getIds() {
		return ids;
	}
	public void setIds(String ids) {
		this.ids = ids;
	}
	public String getIdss() {
		return idss;
	}
	public void setIdss(String idss) {
		this.idss = idss;
	}
	@Action(value="standard_del",results={@Result(name="success",type="redirect",location="/pages/base/standard.html")})
	public String StandardDel(){
		String[] split = ids.split("-");
		standardService.StandardDel(split);
		return SUCCESS;
	}
	//还原操作
	@Action(value="standard_save",results={@Result(name="success",type="redirect",location="/pages/base/standard.html")})
	public String StandardSave(){
		String[] split = ids.split("-");
		standardService.StandardSave(split);
		return SUCCESS;
	}*/

}
